#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       player.py
#       
#       Copyright 2012 Diego Rivas <diego@diego>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#       
#       
import sys, os, time, pygame
from pygame import mixer, key
from pygame.locals import *

class Player():
	
	def __init__(self, frequency = 22050, size = -16, channels = 2, buf = 4096):
		mixer.init(frequency, size, channels, buf)
		
		self.volume = 1.0
		
	def load(self, path):
		mixer.music.load(path)
		
	def play(self):
		mixer.music.play()
		
	def pause(self):
		mixer.music.pause()
		
	def stop(self):
		mixer.music.stop()
		
	def set_volume(n = 1.0):
		if (n > 1):
			self.volume = 1
		elif (n < 0):
			self.volume = 0
		else:
			self.volume = n
			
		mixer.music.set_volume(self.volume)
	
	def inc_volume(self):
		if (self.volume < 1):
			self.volume = self.volume + 0.1
			
		mixer.music.set_volume(self.volume)
		
	def dec_volume(self):
		if (self.volume > 0):
			self.volume = self.volume - 0.1
			
		mixer.music.set_volume(self.volume)
		
	def add_to_queue(self, path):
		mixer.music.queue(path)
		
	def get_position(self):
		return mixer.music.get_pos()
		
	def fadeout(self, time):
		mixer.music.fadeout(time)

	def playing(self):
		return mixer.music.get_busy()
		
	def die(self):
		mixer.quit()
		
	

def main():
	player = Player()
	player.load(sys.argv[1])
	player.play()
	
	while (player.playing()):
		print player.get_position(),
		print "\n"
		
	player.die()
	
	return 0

if __name__ == '__main__':
	main()

